<template>
    <a-modal
            title="编辑"
            :visible="proVisible"
            :width="900"
            :confirm-loading="proLoading"
            @cancel="handleProClose"
            :maskClosable='false'
            @ok="handleProOk"
            class="risk-form-model"
    >
        <div class="add-modal-bus">
            <a-form-model
                    @keyup.enter.native="handleProOk"
                    :model="proItem"
                    :label-col="{ span: 5 }"
                    :wrapper-col="{ span: 19 }"
                    ref="proForm"
                    :rules="proRules"
            >
                <section>
                    <h3>控制活动</h3>
                    <a-form-model-item label="控制活动编号">
                        <a-input placeholder="自动生成" :disabled="true"  v-model="proItem.riskActivityNo"></a-input>
                    </a-form-model-item>
                    <a-form-model-item label="控制活动" prop="riskActivity">
                        <a-textarea placeholder="请输入" allowClear v-model="proItem.riskActivity"></a-textarea>
                    </a-form-model-item>
                    <a-form-model-item label="控制属性" prop="controlAttributeCode">
                        <SysDict v-model="proItem.controlAttributeCode"  codeField="controlAttributeCode" labelField="controlAttributeName" dictType='inc_control_attribute' @valueChange="dictChange" ></SysDict>
                    </a-form-model-item>
                    <a-form-model-item prop="monitorPoint" label="检测点">
                        <a-textarea placeholder="请输入" allowClear v-model="proItem.monitorPoint"></a-textarea>
                    </a-form-model-item>
                    <a-form-model-item label="控制类型" prop="controlTypeCode">

                        <SysDict v-model="proItem.controlTypeCode" codeField="controlTypeCode" labelField="controlType" dictType='control_type'   @valueChange="dictChange"></SysDict>
                    </a-form-model-item>
                    <a-form-model-item label="是否线上控制" prop="isOnlineCode">
                        <a-select
                                style="width: 100%"
                                v-model="proItem.isOnlineCode"
                                :mode="mode"
                                :placeholder="placeholder"
                                :allowClear="true"
                                :dropdownMatchSelectWidth="false"
                        >
                            <a-select-option  v-for="(d, index) in isOnlineList" :key="index" :value="d.value">
                                {{ d.label }}
                            </a-select-option>
                        </a-select>
<!--                        <SysDict v-model="proItem.isOnlineCode" codeField="isOnlineCode" labelField="isOnline" dictType='online_type'   @valueChange="(value,dictData)=>dictChange(value,dictData,'isOnlineCode')"></SysDict>-->
                    </a-form-model-item>
                    <a-form-model-item label="控制系统" prop="controlSystem" v-if="proItem.isOnlineCode == 1">
                        <a-select
                                placeholder="请输入关键字搜索"
                                :filter-option="false"
                                :not-found-content="fetching ? '找不到记录' : null"
                                v-model="proItem.controlSystem"
                                allowClear
                        >
                            <a-spin v-if="fetching" slot="notFoundContent" size="small" />
                            <a-select-option v-for="d in
                            dict.sys" :key="d.name" :value="d.name">
                                {{ d.name }}
                            </a-select-option>
                        </a-select>
                    </a-form-model-item>
                    <a-form-model-item label="审批流名称" prop="approvalName">
                        <a-textarea placeholder="请输入" allowClear v-model="proItem.approvalName"></a-textarea>
                    </a-form-model-item>
                    <a-form-model-item label="关联制度名称" prop="institutionText">
                        <a-input readOnly   @click="addStandard('institutionModalForm','选择相关标准')" v-model="proItem.institutionText" allowClear>
                            <a-icon slot="addonAfter" type="close-circle" @click="clear('institutionText')" />
                        </a-input>

                    </a-form-model-item>
                </section>
            </a-form-model>
            <institutionModal selectType="radio" ref="institutionModalForm" @addRelationTags="addRelationTags('institutionModalForm','institutionTags',{model:'model',key:'id',label:'fileName'})"></institutionModal>
        </div>
    </a-modal>
</template>
<script>
    import { getRiskAction, postAction, getAction, postRiskAction } from '@/api/manage';
    import IncTreeSelect from '@/components/tools/IncTreeSelect';
    import SysDict from '@/components/tools/SysDict';
    import InstitutionModal from '@/components/tools/InstitutionModal'
    import { institutionStatus } from '@/utils/filters';
    export default {
        name: 'AddConlModal',
        components: {
            IncTreeSelect , SysDict , InstitutionModal
        },
        props: {
        },
        data() {
            return {
                disableSubmit: true,
                title: '',
                proVisible: false,
                proLoading: false,
                proItem: {},
                isOnlineList:[
                    {value:0 , label:'否'} ,
                    {value:1 , label:'是'} ,
                ] ,
                controlTypeList:[
                    {value:1 , label:'预防型控制'} ,
                    {value:2 , label:'检查型控制'} ,
                ] ,
                proRules: {
                    riskActivity: [{ required: true, message: '请选择', trigger: 'change' }],
                    controlTypeCode: [{ required: true, message: '请选择', trigger: 'change' }],
                    isOnlineCode: [{ required: true, message: '请输入', trigger: 'blur' }],
                    controlAttributeCode: [{ required: true, message: '请输入', trigger: 'blur' }],
                },
                url: {
                    getDeptTree: '/lib/hteDict/getDeptTree',
                    getGroupList: '/lib/hteLibraryGroup/groupList',
                    getUserList: '/lib/hteFileClass/getUserList',
                    save: '/incActivity/save',
                    dict: '/api/standardDigit/dict',
                    getBusinessModel: '/incBusinessProcessLevel/getBusinessModel'
                },
                // 部门
                treeData: [],
                objData: [],
                fetching: false,
                dict: {}
            };
        },
        mounted() {
            this.initData();
            this.initDict();
        },
        methods: {
            addStandard(ref,name) {
                this.$refs[ref].title = name;
                this.$refs[ref].ipagination.status = [1]
                this.$refs[ref].add();
            },
            addRelationTags(ref,targe) {
                const that = this;
                var rows = this.$refs[ref].selectionRows
                if(!rows || !rows.length==1){
                    this.$message.warning('请选择一份制度', 2);
                    return
                }
                var data = rows[0]
                var statusName =  institutionStatus(data.status)
                this.$set(this.proItem,'institutionText',`${data.fileName}(状态:${statusName})`)
                this.$set(this.proItem,'institutionStatus',`${data.status}`)
                this.$set(this.proItem,'associationInstitution',`${data.fileName}`)
                this.$set(this.proItem,'institutionId',`${data.id}`)
            },
            clear()
            {
                this.$set(this.proItem,'institutionText','')
                this.$set(this.proItem,'institutionStatus','')
                this.$set(this.proItem,'associationInstitution','')
                this.$set(this.proItem,'institutionId',0)
            },
            initDict() {
                let that = this;
                getRiskAction(this.url.dict, { model: 'inc_sys' }).then(res => {
                    if (res.code == 0) {
                        that.dict.sys = res.data;
                    }
                })
            },
            filterOption(input, option) {
                return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
            },
            showModal(){
                this.proVisible = true
            } ,
            setForm(data)
            {
                console.log('数据传入的数据' , data)
                this.initDict();
                this.proItem = data
                if (data.institutionId){
                    var statusName =  institutionStatus(data.institutionStatus)
                    this.$set(this.proItem,'institutionText',`${data.associationInstitution}(状态:${statusName})`)
                }
            },
            dictChange(value,dictData,type){
                this.proItem =  Object.assign(this.proItem,dictData.dataObject)
                delete this.proRules.controlSystem;
                this.$set( this.proItem,'controlSystem',undefined)
                if(value == 1 && type == 'isOnlineCode'){
                    this.proRules.controlSystem =  [{ required: true, message: '请输入', trigger: 'blur' }]
                }
                this.proRules = JSON.parse(JSON.stringify( this.proRules))
            },
            handleIUpload(targe, type) {
                this[targe] = type;
            },
            handleProClose()
            {
                this.proVisible = false
            },
            // 提交
            handleProOk() {
                let that = this;
                this.$refs.proForm.validate(valid => {
                    if (valid) {
                        var param =JSON.parse(JSON.stringify( that.proItem))
                        postRiskAction(that.url.save , param).then( res => {
                            if (res.code ==0){
                                this.$emit('ok');
                                this.handleProClose();
                                that.$message.success(res.msg)
                            }
                        })
                    }
                });
            },
            // 多选
            handleChange(value) {
                console.log(`Selected: ${value}`);
            },
        }
    };
</script>

<style lang="scss" scoped>
    @import '@/styles/riskDetInfo.scss';
    @import '@/styles/riskForm.scss';
    .add-modal-bus {
        h3 {
            position: relative;
            padding-left: 12px;
            font-weight: bold;
            font-size: 16px;
            text-align: left;
            color: #666;
            margin: 10px 0;
            &:before {
                content: '';
                position: absolute;
                top: 10%;
                left: 0;
                width: 4px;
                height: 80%;
                border-radius: 1px;
                background: #5995e3;
            }
        }
    }
</style>
